package com.example.lay.ui;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.example.lay.ui.R;

public class MyImageActivity extends Activity {

    private ImageView imgPart, imgZoom;
    private int[] imgs = {
            R.drawable.libai,
            R.drawable.xiaoming,
            R.drawable.zoom
    };
    int currentImg = 2;
    int alpha = 255;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_image);
        imgPart = (ImageView) findViewById(R.id.imgPart);
        imgZoom = (ImageView) findViewById(R.id.imgZoom);
        imgZoom.setImageResource(imgs[currentImg]);
        imgZoom.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                BitmapDrawable draw = (BitmapDrawable) imgZoom.getDrawable();
                Bitmap bitmap = draw.getBitmap();
                Double scale = bitmap.getWidth() / 320.0;
                int x = (int) (event.getX() * scale);
                int y = (int) (event.getY() * scale);
                if (x + 120 > bitmap.getWidth())
                    x = bitmap.getWidth() - 120;
                if (y + 120 > bitmap.getHeight())
                    y = bitmap.getHeight() - 120;
                imgPart.setImageBitmap(bitmap.createBitmap(bitmap, x, y, 120, 120));
                imgPart.setImageAlpha(alpha);
                return false;
            }
        });

    }

    public void increaseAlpha(View view) {
        if (alpha >= 255)
            alpha = 255;
        else
            alpha += 20;
        imgZoom.setImageAlpha(alpha);
    }

    public void reduceAlpha(View view) {
        if (alpha <= 0)
            alpha = 0;
        else
            alpha -= 20;
        imgZoom.setImageAlpha(alpha);
        System.out.println(alpha);
    }

    public void next(View view) {
        imgZoom.setImageResource(imgs[++currentImg % imgs.length]);
    }
}
